package cn.wolfcode.ssm.controller;

import cn.wolfcode.ssm.domain.Role;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.IPermissionService;
import cn.wolfcode.ssm.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo) {
        PageInfo<Role> pageInfo = roleService.selectForPageResult(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "role/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {
        //在角色编辑页面需要显示所有permission,所以查出共享到role/input.jsp页面
       model.addAttribute("permissions",permissionService.getAll());
        if (id != null) {
            model.addAttribute("role", roleService.getOne(id));
        }
        return "role/input";
    }


    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            roleService.delete(id);
        }
        return "redirect:/role/list.do";
    }


    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids) {//ids 权限id的数组
        if (role.getId() != null) {
            roleService.update(role,ids);
        } else {
            roleService.save(role,ids);
        }
        return "redirect:/role/list.do";
    }

}
